package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.constant.HttpStatus;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private ITbClueService clueService;


    /**
     * 根据线索id查询跟进记录数据
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo getByClueId(Long clueId) {
        List<ClueTrackRecordVo> trackRecordList = clueTrackRecordMapper.getByClueId(clueId);
        Integer total = clueTrackRecordMapper.getTotalById(clueId);
        TableDataInfo tableDataInfo = new TableDataInfo(trackRecordList, total);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        return tableDataInfo;
    }

    /**
     * 添加跟进线索数据
     * @param clueTrackRecord
     * @return
     */
    @Override
    public Integer saveClueTrackRecord(ClueTrackRecordVo clueTrackRecord) {
        //1、更新线索数据
        TbClue tbClue = new TbClue();
        // 拷贝属性
        BeanUtils.copyProperties(clueTrackRecord,tbClue);
        tbClue.setId(clueTrackRecord.getClueId());
        // 更新线索数据
        int updateTbClueRow = clueService.updateTbClue(tbClue);

        // 2、添加线索跟进记录数据
        TbClueTrackRecord trackRecord = new TbClueTrackRecord();
        // 拷贝属性
        BeanUtils.copyProperties(clueTrackRecord,trackRecord);
        // 设置跟进人
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        // 设置跟进时间
        trackRecord.setCreateTime(DateUtils.getNowDate());
        // 添加跟进记录数据
        Integer row = clueTrackRecordMapper.saveClueTrackRecord(trackRecord);

        return row;
    }

}
